package com.tqfframe.rabbitmq.config;

import com.tqfframe.rabbitmq.constants.RabbitConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延迟队列配置
 * @author: Qifeng Tang
 * @date: 2022/11/17 10:14
 * @Version 1.0
 **/
@Configuration
public class RabbitMqDelayConfig {

    //提供一个消息队列 Queue
    @Bean
    public Queue delayQueue() {
        return QueueBuilder.durable(RabbitConstants.DELAY_QUEUE).build();
    }

    //创建延迟交换机
    @Bean
    public Exchange  delayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(RabbitConstants.DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    // 延迟交换机绑定队列
    @Bean
    public Binding delayPayBind() {
        return BindingBuilder.bind(delayQueue())
                .to(delayExchange()).with(RabbitConstants.DELAY_ROUTINGKEY).noargs();
    }

    //使用FanoutExchange交换机创建延迟队列
    
//    //提供一个消息队列 Queue
//    @Bean
//    public Queue delayQueue() {
//        return new Queue(RabbitConstants.DELAY_QUEUE, true);
//    }
//
//    //创建延迟交换机
//    @Bean
//    public FanoutExchange delayExchange() {
//        Map<String, Object> args = new HashMap<>();
//        args.put("x-delayed-type", "direct");
//        FanoutExchange fanoutExchange = new FanoutExchange(RabbitConstants.DELAY_EXCHANGE, true, false, args);
//        fanoutExchange.setDelayed(true);
//        return fanoutExchange;
//    }
//
//    // 延迟交换机绑定队列
//    @Bean
//    public Binding delayPayBind() {
//        return BindingBuilder.bind(delayQueue()).to(delayExchange());
//    }




}
